package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.eduservice.client.OrdersClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.Jwt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Miracle yaochengwei on 2020/9/8 19:56
 *    @RequestBody(required = false) CourseFrontVo courseFrontVo) {
 *    不添加required = false，CourseFrontVo courseFrontVo（实体类，没有值会报错）
 *    添加required = false，CourseFrontVo courseFrontVo
 *    没有值不报错
 */
@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;
    //微服务调用，需要调用orsersCline，先注入
    @Autowired
    private OrdersClient ordersClient;
    //1.条件查询带分页，查询课表   //条件查询，关联刚才建立的实体类
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);//分页对象，条件对象
        //传入map集合
        //返回分页所有数据
        return R.ok().data(map);
    }
    //2.课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息 /返回新建的实体类
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id 查询章节和小节  /chapterVideoList返回对象里面有所有的章节和小节 /这个调用之前写过的方法
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
         //根据课程id和用户id查询当前课程是否已经支付过了
         //用户信息，放在，request的token里，需要，用iwt工具从里面获取用户信息
        //String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        //通过request加jwt从header中取到用户id传进去
        Boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        //Booleanw布尔类型，返回到前端进行判断
        //得到一个对象，一个集合返回到前端页面 中
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",buyCourse);
    }
    //根据课程id 查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        //getBaseCourseInfo调用这个查询课程基本信息的方法
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
